/*
   Copyright 2008, Andres Postiglioni

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.aconcagua.mvc.factory {
  import com.aconcagua.beans.BeanDefinitionRegistrar;
  import com.aconcagua.beans.BeanDefinitionSupport;
  import com.aconcagua.beans.BeanList;
  import com.aconcagua.beans.ConstructorArg;
  import com.aconcagua.beans.Property;
  import com.aconcagua.factory.BeanFactory;
  import com.aconcagua.mvc.control.Controller;

  import mx.core.IMXMLObject;

  /**
   * @author Andres Postiglioni
   */
  public class Controller implements BeanDefinitionRegistrar, IMXMLObject {
    private var _bean: BeanDefinitionSupport;

    private var _eventDispatcher: Object;
    private var _commandFactory: Object;
    private var _mappings: Array;
    private var _mediators: Array; //of Mediator
    private var _id: String;
    private var _type: Class;
    private var _commandExecutor: Object;

    public function Controller() {
      _type = com.aconcagua.mvc.control.Controller;
    }

    public function set type(aClass: Class): void {
      _type = aClass;
    }

    public function set commandExecutor(executor: Object): void {
      _commandExecutor = executor;
    }

    public function initialized(document:Object, id:String): void {
      _id = id;
    }

    public function registerAlias(name: String, beanFactory: BeanFactory): void {
      beanFactory.registerAlias(name, _bean);
    }

    public function  registerIn(beanFactory: BeanFactory): void {
      _bean = new BeanDefinitionSupport(_type, _id);
      _bean.scope = BeanDefinitionSupport.SCOPE_SINGLETON;
      _bean.addConstructorArg(new ConstructorArg(_eventDispatcher));
      _bean.addConstructorArg(new ConstructorArg(_commandFactory));
      _bean.addProperty(new Property("mappings", new BeanList(this.collectMappings())));
      if (_commandExecutor != null) {
        _bean.addProperty(new Property("commandExecutor", _commandExecutor));
      }

      beanFactory.registerBeanDefinition(_bean);
    }

    public function isRegisteredIn(beanFactory: BeanFactory): Boolean {
      return beanFactory.containsBean(_bean.name);
    }

    public function retrieveFrom(beanFactory: BeanFactory): Object {
      return beanFactory.getBean(_bean.name);
    }

    public function set eventDispatcher(dispatcher: Object): void {
      _eventDispatcher = dispatcher;
    }

    public function set commandFactory(factory: Object): void {
      _commandFactory = factory;
    }

    /**
     * @param array array of FactoryBean that creates
     *              com.aconcagua.mvc.control.GestureMapping objects
     **/
    public function set mappings(array: Array): void {
      _mappings = array;
    }

    /**
     * @param array array of com.aconcagua.mvc.factory.Mediator
     **/
    public function set mediators(array: Array): void {
      _mediators = array;
    }

    private function collectMappings(): Array {
      var theMappings: Array = new Array();

      for each(var mapping: GestureMapping in _mappings) {
        theMappings.push(mapping);
      }

      for each(var mediator: Mediator in _mediators) {
        mediator.collectMappings(theMappings);
      }

      return theMappings;
    }
  }
}